package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {


    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 图片上传
     *
     * @param
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile file) {
        try {
            //0.重新定义后缀名
            String originalFilename = file.getOriginalFilename();//获取原始名称
            //截取后缀名
            int i = originalFilename.lastIndexOf(".");
            String suffix = originalFilename.substring(i);
            String filename = UUID.randomUUID().toString() + suffix;
            //1.重新上传图片到minio中，返回路径地址
            String path = fileStorageService.uploadImgFile("", filename, file.getInputStream());

            //2.新增到wm_material表
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setType((short) 0);
            wmMaterial.setUrl(path);
            wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
            save(wmMaterial);
            //3.返回数据
            return ResponseResult.okResult(wmMaterial);

        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.UPLOAD_FAULT);
        }

    }

    /**
     * 素材列表查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //0.检查参数
        dto.checkParam();

        //1.分页查询
        IPage page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper=new LambdaQueryWrapper<>();

        //是否收藏
        if(dto.getIsCollection()!=null && dto.getIsCollection()==1){
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        //按照用户查询
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());

        //按照时间排序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        page=page(page,queryWrapper);

        //2.结果返回
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 素材图片删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult delPicture(Integer id) {
        //ID不存在
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数失效");
        }
        LambdaQueryWrapper<WmMaterial> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(WmMaterial::getId,id);
        WmMaterial wmMaterial = getOne(queryWrapper);
        if(wmMaterial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"数据不存在");
        }
        boolean remove = this.remove(queryWrapper);
        if(remove){
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,"操作成功");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文件删除失败");
        }
    }

    /**
     * 取消收藏
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancelCollect(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数失效");
        }
        LambdaQueryWrapper<WmMaterial> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getId,id);
        WmMaterial wmMaterial = getOne(queryWrapper);
        wmMaterial.setIsCollection((short) 0);
        updateById(wmMaterial);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,"操作成功");
    }

    /**
     * 收藏
     * @param id
     * @return
     */
    @Override
    public ResponseResult Collect(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数失效");
        }
        LambdaQueryWrapper<WmMaterial> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getId,id);
        WmMaterial wmMaterial = getOne(queryWrapper);
        wmMaterial.setIsCollection((short) 1);
        updateById(wmMaterial);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,"操作成功");
    }
}
